import hasRole from './permission/hasRole';
import hasPermi from './permission/hasPermi';
import dialogDrag from './dialog/drag';
import dialogDragWidth from './dialog/dragWidth';
import dialogDragHeight from './dialog/dragHeight';
import clipboard from './module/clipboard';
import Vue from 'vue';

const install = function (Vue) {
	Vue.directive('hasRole', hasRole);
	Vue.directive('hasPermi', hasPermi);
	Vue.directive('clipboard', clipboard);
	Vue.directive('dialogDrag', dialogDrag);
	Vue.directive('dialogDragWidth', dialogDragWidth);
	Vue.directive('dialogDragHeight', dialogDragHeight);
	Vue.directive('thousand', {
		// 指令的定义
		inserted: function (el, binding) {
			// 指令作用在 element-input 节点，对应原生 div.el-input (真实input节点的父节点)
			if (el.tagName.toLocaleUpperCase() === 'SPAN') {
				if (!el.innerText || el.innerText === '--') {
					el.innerText = '--';
				} else {
					el.innerText = parseFloat(el.innerText).toLocaleString(
						'en-US',
						{ minimumFractionDigits: 2, maximumFractionDigits: 2 },
					);
				}
			} else if (el.tagName.toLocaleUpperCase() !== 'INPUT') {
				el = el.getElementsByTagName('input')[0];
			}
			// 带¥符号、保留2位小数+千分位
			// el.value = (Number(el.value)).toLocaleString('zh', {style:'currency', currency: 'CNY', minimumFractionDigits: 2});
			// 保留2位小数+千分位
			el.onblur = e => {
				// console.log('onblur出发了 ')
				if (!el.value) return 0;
				el.value = parseFloat(el.value).toLocaleString('en-US', {
					minimumFractionDigits: 2,
					maximumFractionDigits: 2,
				});
			};
			el.onfocus = e => {
				if (!el.value) return 0;
				const str = el.value.replace(/,/g, '');
				el.value = parseFloat(str);
			};
			if (!el.value) return 0;
			el.value = parseFloat(el.value).toLocaleString('en-US', {
				minimumFractionDigits: 2,
				maximumFractionDigits: 2,
			});
		},
	});
};

if (window.Vue) {
	window['hasRole'] = hasRole;
	window['hasPermi'] = hasPermi;
	Vue.use(install); // eslint-disable-line
}

export default install;
